import PySimpleGUI as sg
from selenium import webdriver
from bs4 import BeautifulSoup
import time

browser = webdriver.Chrome()
browser.get('https://vmware-gs.lightning.force.com/c/GSS_COMMIT.app')

sg.popup(
  '请在浏览器中输入动态密码，并选择要获取的条件，页面加载完成后点击继续按钮',
  title='提示',
  custom_text=' 继续呗'
)

lkeys=['EA Name','Contact Name','Case Number','Case Owner Alias','Product Name','Entitlement Code','Severity','Status','Sub Status','Case Origin','Country','Date/Time Opened','First Response Due Date','Subject','First Response Met','Action']

print('数据获取中，请稍后...')
while True:
    iframe = browser.find_element_by_tag_name('iframe')
    browser.switch_to_frame(iframe)
    soup = BeautifulSoup(browser.page_source)
    # 将browser.page_source写入文件
    with open('output.html', 'w', encoding='utf-8') as f:
        f.write(browser.page_source)
    all_ret=[]
    for tr in soup.find_all('tr'):
        print('TR',tr)
        d={}
        for idx,td in enumerate(tr.find_all('td')[1:]):
            print('TD',td.text.strip())
            d[lkeys[idx]]=td.text.strip()
        # 如果d不为空，就添加到all_ret中
        if d:
            all_ret.append(d)
    browser.switch_to_parent_frame()
    print(all_ret)
    # 刷新页面
    browser.refresh()
    # 等待30秒
    time.sleep(30)
